package com.mnnu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mnnu.anno.Log;
import com.mnnu.mapper.EmpMapper;
import com.mnnu.pojo.Emp;
import com.mnnu.pojo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpService implements com.mnnu.service.EmpService {

    @Autowired
    private EmpMapper empMapper;


    /*
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        Long count = empMapper.count();

        List<Emp> empList = empMapper.page((page - 1) * pageSize, pageSize);

        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }
    */

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);

        //2.执行查询
        List<Emp> empList = empMapper.page(name, gender, begin, end);
        Page<Emp> pageList = (Page<Emp>) empList;

        //3.封装PageBean
        return new PageBean(pageList.getTotal(), pageList.getResult());

    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void add(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.add(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }

}
